package com.ruoyi.order.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.order.es.OrderEs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.document.Document;
import org.springframework.stereotype.Service;
import com.ruoyi.order.mapper.TbOrderMapper;
import com.ruoyi.order.domain.TbOrder;
import com.ruoyi.order.service.ITbOrderService;

/**
 * 订单主Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-08-18
 */
@Service
public class TbOrderServiceImpl implements ITbOrderService 
{
    @Autowired
    private TbOrderMapper tbOrderMapper;

    @Autowired
    ElasticsearchRestTemplate elasticsearchRestTemplate;

    /**
     * 查询订单主
     * 
     * @param orderId 订单主主键
     * @return 订单主
     */
    @Override
    public TbOrder selectTbOrderByOrderId(Long orderId)
    {
        return tbOrderMapper.selectTbOrderByOrderId(orderId);
    }

    /**
     * 查询订单主列表
     * 
     * @param tbOrder 订单主
     * @return 订单主
     */
    @Override
    public List<TbOrder> selectTbOrderList(TbOrder tbOrder)
    {
        return tbOrderMapper.selectTbOrderList(tbOrder);
    }

    /**
     * 新增订单主
     * 
     * @param tbOrder 订单主
     * @return 结果
     */
    @Override
    public int insertTbOrder(TbOrder tbOrder)
    {
        tbOrder.setCreateTime(DateUtils.getNowDate());
        return tbOrderMapper.insertTbOrder(tbOrder);
    }

    /**
     * 修改订单主
     * 
     * @param tbOrder 订单主
     * @return 结果
     */
    @Override
    public int updateTbOrder(TbOrder tbOrder)
    {
        return tbOrderMapper.updateTbOrder(tbOrder);
    }

    /**
     * 批量删除订单主
     * 
     * @param orderIds 需要删除的订单主主键
     * @return 结果
     */
    @Override
    public int deleteTbOrderByOrderIds(Long[] orderIds)
    {
        return tbOrderMapper.deleteTbOrderByOrderIds(orderIds);
    }

    /**
     * 删除订单主信息
     * 
     * @param orderId 订单主主键
     * @return 结果
     */
    @Override
    public int deleteTbOrderByOrderId(Long orderId)
    {
        return tbOrderMapper.deleteTbOrderByOrderId(orderId);
    }

    @Override
    public AjaxResult createOrderIndex() {
        IndexOperations indexOps = elasticsearchRestTemplate.indexOps(OrderEs.class);
        boolean exists = indexOps.exists();
        if(exists){
            indexOps.delete();
        }
        indexOps.create();
        Document mapping = indexOps.createMapping();
        indexOps.putMapping(mapping);
        return AjaxResult.success("创建成功");
    }

    @Override
    public AjaxResult syncOrderIndex() {
        List<TbOrder> tbProds=tbOrderMapper.selectTbOrderList(new TbOrder());

        for (TbOrder tbProd : tbProds) {
            OrderEs esProd = BeanUtil.toBean(tbProd, OrderEs.class);
            elasticsearchRestTemplate.save(esProd);
        }
        return AjaxResult.success("成功");
    }

    @Override
    public TbOrder getByOrderNo(String orderNo) {

        return tbOrderMapper.selectByOrderNo(orderNo);

    }

    @Override
    public int updateById(TbOrder order) {
        return tbOrderMapper.updateTbOrder( order);
    }
}
